import time
import argparse
import TrosParser.libs.dbcs_parser as dbcs_parser
import TrosParser.libs.log as log
import TrosParser.libs.gen_proto as gen_proto
import TrosParser.libs.gen_canjson as gen_canjson
import TrosParser.libs.gen_struct as gen_struct
import os
import shutil

if __name__ == "__main__":

    print('-----------------------------------------------')
    print('start generating proto file, json file and struct file')
    timestamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    print('Started:',timestamp)
    print('-----------------------------------------------')
    parser = argparse.ArgumentParser(description='')
    parser.add_argument('-dp',
                        '--dbc_path',
                        nargs=1,
                        type=str,
                        default=None,
                        help="input dbc files path")              
    args = parser.parse_args()


    t_dbcs_path = None
    if None != args.dbc_path:
        t_dbcs_path = args.dbc_path[0]
        print("t_dbcs_path: ", t_dbcs_path)
    else:
        parser.print_help()
        exit(-1)


    dirs = ['output']
    for dir in dirs:
        if os.path.exists(dir):
            # 如果目录存在，删除它
            shutil.rmtree(dir)
        # 重新创建目录
        os.makedirs(dir)

    logging = log.setup_logger('log','output/log.txt')
    dp = dbcs_parser.dbcs_parser()
    dp.parse_dbcs(t_dbcs_path, logging)



    gen_proto.gen(dp,logging)    
    print('-----------------------------------------------')
    print('proto generation DONE')
    timestamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    print('Ended:',timestamp)
    print('-----------------------------------------------\n')

    gen_canjson.gen_can_json(dp,logging)
    print('-----------------------------------------------')
    print('json generation DONE')
    timestamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    print('Ended:',timestamp)
    print('-----------------------------------------------')

    gen_struct.gen_struct(dp,logging)
    print('-----------------------------------------------')
    print('struct generation DONE')
    timestamp = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    print('Ended:',timestamp)
    print('-----------------------------------------------')